<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Mcampaignagencyplace extends MY_Model
{
    public function __construct()
    {
        parent::__construct();
    }
    
    public function camPaignPlace($number,$offset,$id, $id_place)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id AS uid, c.id_customers, c.title, DATE_FORMAT(c.time_start, '%d-%m-%Y') AS time_start, DATE_FORMAT(c.time_end, '%d-%m-%Y') AS time_end, c.link_campaign, c.note, c.folder_campaign, c.active, c.created, c.id_agency, u.username, c.id AS cid, cp.id_campaign, cp.id_place
                FROM campaign AS c
                INNER JOIN `user` AS u ON c.id_customers = u.id
                INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign
                WHERE 1 = 1 AND c.id_agency = ".$id." AND cp.id_place = ".$id_place;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND c.title like '%".$key."%' ";
        }
        $sql .= " ORDER BY c.id ASC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
    
    public function placeDB($number,$offset,$id)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT s.shopName, s.address, s.mobile, s.logo, s.ip, s.lat, s.`long`, s.MaC, cp.id, cp.id_place, cp.id_campaign, s.id AS ssid, IF(cp.id_place IS NOT NULL, Count(s.id), 0) AS num FROM campaign_place AS cp LEFT JOIN campaign ON cp.id_campaign = campaign.id RIGHT JOIN shop AS s ON cp.id_place = s.id WHERE 1 = 1 OR campaign.id_agency = ".$id." GROUP BY s.id ";
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (s.shopName like '%".$key."%' OR ";
            $sql .= " s.address like '%".$key."%') ";
        }
        $sql .= " ORDER BY s.shopName ASC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function getCampaignAgencyDB($number,$offset,$id)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id AS uid, c.code_campaign, c.id_customers, c.title, DATE_FORMAT(c.time_start, '%d-%m-%Y') AS time_start, DATE_FORMAT(c.time_end, '%d-%m-%Y') AS time_end,c.link_campaign, c.note, c.folder_campaign, c.active, c.created, c.id_agency, u.username, c.id AS cid FROM campaign AS c INNER JOIN `user` AS u ON c.id_customers = u.id WHERE 1=1 AND c.active = 2 AND c.id_agency = ".$id;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.title like '%".$key."%' OR ";
            $sql .= " c.code_campaign like '%".$key."%') ";
        }
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function getCampaignAgencyPenddingDB($number,$offset,$id)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id AS uid, c.code_campaign, c.id_customers, c.title, DATE_FORMAT(c.time_start, '%d-%m-%Y') AS time_start, DATE_FORMAT(c.time_end, '%d-%m-%Y') AS time_end,c.link_campaign, c.note, c.folder_campaign, c.active, c.created, c.id_agency, u.username, c.id AS cid FROM campaign AS c INNER JOIN `user` AS u ON c.id_customers = u.id WHERE 1=1 AND c.active = 0 AND c.id_agency = ".$id;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.title like '%".$key."%' OR ";
            $sql .= " c.code_campaign like '%".$key."%') ";
        }
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
    public function getCampaignAgencysendDB($number,$offset,$id)
    {
        $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id AS uid, c.code_campaign, c.id_customers, c.title, DATE_FORMAT(c.time_start, '%d-%m-%Y') AS time_start, DATE_FORMAT(c.time_end, '%d-%m-%Y') AS time_end,c.link_campaign, c.note, c.folder_campaign, c.active, c.created, c.id_agency, u.username, c.id AS cid FROM campaign AS c INNER JOIN `user` AS u ON c.id_customers = u.id WHERE 1=1 AND c.active = 1 AND c.id_agency = ".$id;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.title like '%".$key."%' OR ";
            $sql .= " c.code_campaign like '%".$key."%') ";
        }
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
        
    }
    
	public function getListShop($id)
    {
        $data = $this->db->query('SELECT id, shopName, address FROM shop ORDER BY id ASC')->result_object();
        $html = '<option value = "0">Chọn cửa hàng</option>';
        foreach($data AS $el=>$le)
        {
            if($le->id == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$le->id.'" '.$selected.'>'.$le->shopName.'-'.$le->address.'</option>';
        }
        return $html;
    }
	
	public function addCampaignUser(){
	
	}
    
    public function viewCampaignAgencyDB($id){
        if(in_array($id, array(null,'')))
        {
            redirect('');
        }
        $data = $this->db->query("SELECT ca.id,ca.title,ca.active,ca.status_old,DATE_FORMAT(ca.time_start,'%H:%i:%s %d-%m-%Y') AS time_start,DATE_FORMAT(ca.time_end,'%H:%i:%s %d-%m-%Y') AS time_end,ca.link_campaign,ca.note,u.fullname AS customer,u.id AS customer_id FROM campaign AS ca LEFT JOIN user AS u ON ca.id_customers = u.id WHERE ca.id = $id")->row_object();

        $result = array();
        $location = $this->db->query("SELECT lc.id AS p_id,cp.id AS c_id,lc.shopName,lc.address FROM campaign_place AS cp LEFT JOIN shop AS lc ON cp.id_place = lc.id WHERE cp.id_campaign = ".$data->id)->result_object();
        $data->location = (object)$location;
        $result = $data;
     
        return $result;
    }
    
    public function getCampaignAgencyOtherDB($number,$offset,$id)
    {
         $limit = $offset ? intval($offset) : '0';
        
        $sql = "SELECT u.id AS uid, c.code_campaign, c.id_customers, c.title, DATE_FORMAT(c.time_start, '%d-%m-%Y') AS time_start, DATE_FORMAT(c.time_end, '%d-%m-%Y') AS time_end,c.link_campaign, c.note, c.folder_campaign, c.active, c.created, c.id_agency, u.username, c.id AS cid FROM campaign AS c INNER JOIN `user` AS u ON c.id_customers = u.id WHERE 1=1 AND (c.active = 3 OR c.active = 4)  AND c.id_agency = ".$id;
        $key = $this->getParamString('keyword');
        if(!in_array($key, array(null, '', '0')))
        {
            $sql .= " AND (c.title like '%".$key."%' OR ";
            $sql .= " c.code_campaign like '%".$key."%') ";
        }
        $sql .= " ORDER BY c.id DESC";
        $xdata = " LIMIT ".$limit.",".$number." ";
        
        $xs = $this->db->query($sql.$xdata)->result_object();
        $count = $this->db->query($sql)->num_rows();
        return array('data' => $xs, 'count' => $count);
    }
	
    public function ajaxLockCampaignDB($id)
    {
        $active = $this->db->query("SELECT active FROM campaign WHERE id = $id")->row_object()->active;
        if(in_array($active, array(0,1,2)) ){
            $agency = $this->db->query("SELECT id_agency FROM campaign WHERE id = $id")->row_object();
            if($agency->id_agency == $this->session->userdata('id_user'))
            {
                $data = array(
                    'status_old' => $active,
                    'active' => 3,
                    'id_update' => $this->session->userdata('id_user')
                );
                $this->db->update('campaign', $data, "id = $id");
                return 200;
            }
        }
        return 201;
    }
    
    public function ajaxCancelCampaignDB($id)
    {
        $active = $this->db->query("SELECT active FROM campaign WHERE id = $id")->row_object()->active;
        if(in_array($active, array(0,1,2,3)) ){
            $agency = $this->db->query("SELECT id_agency FROM campaign WHERE id = $id")->row_object();
            if($agency->id_agency == $this->session->userdata('id_user'))
            {
                $data = array(
                    'status_old' => $active,
                    'active' => 4,
                    'id_update' => $this->session->userdata('id_user')
                );
                $this->db->update('campaign', $data, "id = $id");
                return 200;
            }
        }
        return 201;
    }
    
    public function ajaxSendApprovalCampaignDB($id)
    {
        $active = $this->db->query("SELECT active FROM campaign WHERE id = $id")->row_object()->active;
        if(in_array($active, array(0)) ){
            $agency = $this->db->query("SELECT id_agency FROM campaign WHERE id = $id")->row_object();
            if($agency->id_agency == $this->session->userdata('id_user'))
            {
                $data = array(
                    'status_old' => $active,
                    'active' => 1,
                    'id_update' => $this->session->userdata('id_user')
                );
                $this->db->update('campaign', $data, "id = $id");
                return 200;
            }
        }
        return 201;
    }
    
    public function ajaxRestoreCampaignDB($id)
    {
        $get = $this->db->query("SELECT active,status_old FROM campaign WHERE id = $id")->row_object();
        if(in_array($get->active, array(3,4)) ){
            $agency = $this->db->query("SELECT id_agency FROM campaign WHERE id = $id")->row_object();
            if($agency->id_agency == $this->session->userdata('id_user'))
            {
                $data = array(
                    'status_old' => $get->active,
                    'active' => $get->status_old,
                    'id_update' => $this->session->userdata('id_user')
                );
                $this->db->update('campaign', $data, "id = $id");
                return array('status' => 200,'active' => $get->status_old);
            }
        }
        return array('status' => 201,'active' => $get->active);
    }
    
    public function getListActive($id,$status_old)
    {
        if($id == 0){
            $data_status = array( '1' => 'Gửi duyệt', '4' => 'Hủy bỏ' );
        }else if($id == 1){
            $data_status = array( '4' => 'Hủy bỏ' );
        }else if($id == 2){
            $data_status = array( '3' => 'Tạm dừng', '4' => 'Hủy bỏ' );
        }else if($id == 3){
            $data_status = array( '2' => 'Chạy tiếp', '4' => 'Hủy bỏ' );
        }else if($id == 4){
            if(in_array($status_old , array(0,1,3)) ){
                $data_status = array( ''.$status_old.'' => 'Khôi phục trạng thái cũ' );    
            }else{
                $data_status = array( '2' => 'Khôi phục và chạy tiếp');   
            }
        }
        $html = '<option value="'.$id.'">Chọn trạng thái</option>';
        //$html = '';
        foreach($data_status AS $el=>$le)
        {
            if($el == $id)
            {
                $selected = 'selected="selected"';
            }else{
                $selected = '';
            }
            $html .= '<option value="'.$el.'" '.$selected.'>'.$le.'</option>';
        }
        return $html;
    }
    
    
    public function updateCampaign($status,$id)
    {
        $get = $this->db->query("SELECT active,status_old FROM campaign WHERE id = $id")->row_object();
        if($status != $get->active)
        {
            $data = array(
                'status_old' => $get->active,
                'active' => $status,
                'id_update' => $this->session->userdata('id_user')
            );
            $this->db->update('campaign', $data, "id = ". $id);
        } 
        
        return 200;
    }
    
    public function getCountAgency($id_user)
    {
        $sql = "SELECT c.*,DATE_FORMAT(c.created, '%H:%i:%s %d-%m-%Y') AS time FROM campaign AS c JOIN campaign_place AS cp ON c.id = cp.id_campaign WHERE cp.id_place = ".$this->session->userdata('place');
        $sql .= " ORDER BY id DESC";
        $xs = $this->db->query($sql)->result_object();
        //echo '<pre>';die(print_r($xs));
        $id = array();
        $array = array();
        $dem = 0;
        $key = '';
        $array_device = array();
        $dem_device = 0;
        $key_device = '';
        
        $array_os = array();
        $dem_os = 0;
        $key_os = '';
        
        foreach($xs AS $el=>$le)
        {
            $id[] = intval($le->id);
        }
        //echo '<pre>';die(print_r($id));
        $data = $this->m_db->where_in('id_campaign', $id)->where('id_shop', $this->session->userdata('place') )->get('log_access');
        
        foreach($data AS $le=>$el)
        {
            if($el['browser_name_group'] == 'Other')
            {
                $dem =  $dem + 1;
                $key = 'Other';
            }else if(in_array($el['browser_name_group'], array(null, '', '0')))
            {
                $dem =  $dem + 1;
                $key = 'Other';
            }else{
                $dem =  $dem + 1;
                $key = '';
                $key = $el['browser_name_group'];
            }
            @$array[$key] = $array[$key] + 1;
            
            
            if($el['device_name'] == 'Other')
            {
                $dem_device =  $dem_device + 1;
                $key_device = 'Máy tính';
            }else{
                $dem_device =  $dem_device + 1;
                $key_device = 'Di động';
            }
            @$array_device[$key_device] = $array_device[$key_device] + 1;
            
            if(in_array($el['os_group'], array(null, '', '0')))
            {
                $dem_os =  $dem_os + 1;
                $key_os = 'Other';
            }else{
                $dem_os =  $dem_os + 1;
                $key_os = '';
                $key_os = $el['os_group'];
            }
            @$array_os[$key_os] = $array_os[$key_os] + 1;
        }
        $array['tong_browser'] = $dem;
        asort($array);
        $array_device['tong_device'] = $dem_device;
        asort($array_device);
        $array_os['tong_os'] = $dem_os;
        asort($array_os);
        
        return (object)array('browser' => $array,'device' => $array_device, 'os' => $array_os);
    }
    
    public function getCampaignReport()
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop 
        FROM campaign AS c 
            LEFT JOIN `user` AS u ON u.id = c.id_agency 
            INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign 
            INNER JOIN shop AS s ON cp.id_place = s.id 
        WHERE (c.active >= 2) AND cp.id_place = ".$this->session->userdata('place');
        $sql .= " ORDER BY c.id DESC LIMIT 0,3";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
             $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
             $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
            $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
            $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
        }
        return array('data' => $xs);
    }
    
    public function ajaxGetCampaignReport($last_id)
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop 
        FROM campaign AS c 
            LEFT JOIN `user` AS u ON u.id = c.id_agency 
            INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign 
            INNER JOIN shop AS s ON cp.id_place = s.id 
        WHERE (c.active = 2 OR c.active = 5) AND cp.id_place = ".$this->session->userdata('place');
        $sql .= " ORDER BY c.id DESC LIMIT $last_id,3";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
             $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
             $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
            $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
            $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
        }
        return array('data' => $xs);
    }
    
    public function getPlace()
    {
		$timeout = $this->db->query('SELECT content FROM `option` WHERE name ="timeout"')->row_object()->content; $timeout = (int)$timeout;
		$place_data = $this->db->query('SELECT s.id, s.shopName AS placeName, s.address, s.lat, s.`long`, s.logo, s.modem_id, s.MaC, m.time, m.info_ram, m.info_cpu, m.info_users FROM shop AS s JOIN modem AS m ON s.MaC = m.nas_id WHERE s.id = '.$this->session->userdata('place'))->row_object();
		//die(''.print_r($place_data));
        $place_data->campaign = $this->db->query('SELECT c.id AS c_id, cp.id_place AS p_id, c.title FROM campaign AS c INNER JOIN campaign_place AS cp ON cp.id_campaign = c.id WHERE c.active >= 2 AND cp.id_place = '.$this->session->userdata('place'))->result_object();
		$db_modem = $this->db->query('SELECT time AS times FROM modem WHERE nas_id = "'.$place_data->MaC.'" LIMIT 1')->row();
		//echo '<pre>';die(print_r($db_modem));
		if ( (int)($db_modem->times) + $timeout > time() ){
                $place_data->status_modem = 'Đang hoạt động';
                $place_data->icon_map = 'public/theme/analytics/images/map-maker-run.png';
            }else{
                $place_data->status_modem = 'Ngừng hoạt động';
                $place_data->icon_map = 'public/theme/analytics/images/map-maker-stop.png';
            }
        return $place_data;
    }
    
    public function getReportExport()
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE (c.active >= 2 AND c.active != 4) AND cp.id_place = ".$this->session->userdata('place');
        $sql .= " ORDER BY c.id DESC";
        $xs = $this->db->query($sql)->result_object();
        foreach ( $xs AS $el=>$le ){
            $dem = 0;
            $dem_user = 0;
            $tmp = array();
            $tmp['mac'] = '';
            $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
            foreach($total AS $ell=>$lle)
            {
                // Đếm tổng
                $dem = $dem + 1;
                if($lle['mac'] != $tmp['mac'])
                {
                    // số người dùng
                    $tmp['mac'] = $lle['mac'];
                    $dem_user = $dem_user + 1;
                }
            }
            
            $xs[$el]->total_visit = $dem;
            $xs[$el]->user_visit = $dem_user;
            
            $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
            $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
            $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
            $click = $total_facebook + $total_mail + $total_mobile;
            
            @$outClick = round((($dem - $click) / $dem) * 100);
            @$_click = round(($click / $dem) * 100);
            
            $xs[$el]->personNoClick = $outClick;
            $xs[$el]->personClick = $_click;
            
            /** get tong thơi zan trang dic và so lan hien thi trang dic*/
            $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
            $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
            $xs[$el]->landing_page = $_data_landing_page->landing_page;
            $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
            $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
        }
        return array('data' => $xs);
    }
    
    public function getAjaxReportExport($times,$times_start,$times_end,$customer_id)
    {
        $sql = "SELECT c.id,c.code_campaign,c.id_customers,c.title AS campaign_title,c.active, c.id_agency, c.link_campaign, c.status_old, c.id_update, c.created, u.fullname AS agency_fullname, CONCAT('<b>',s.shopName,'</b>',' - ',s.address) AS nameplace, s.id AS id_shop FROM campaign AS c LEFT JOIN `user` AS u ON u.id = c.id_agency INNER JOIN campaign_place AS cp ON c.id = cp.id_campaign INNER JOIN shop AS s ON cp.id_place = s.id WHERE c.active != 4 AND c.id_agency = ".$this->session->userdata('id_user');
        if(!in_array($times_start, array(null,'','0',0))){
            $sql .= " AND (c.time_start BETWEEN '$times_start' AND '$times_end') OR (c.time_end BETWEEN '$times_start' AND '$times_end') OR ('$times_start' AND '$times_end' BETWEEN c.time_start AND c.time_end)";   
        }
        if(!in_array($customer_id, array(null,'','0',0))){
            $sql .= " AND c.id_customers = ".$customer_id;
        }
        $sql .= " ORDER BY c.id DESC";
        $xs = $this->db->query($sql)->result_object();
        
        if(!in_array($times, array(null, '', '0')))
        {
            $exp = explode('|',$times);
            $time_1 = new MongoInt64($exp[0]);
            $time_2 = new MongoInt64($exp[1]);
            
            
            foreach ( $xs AS $el=>$le ){
                $dem = 0;
                $dem_user = 0;
                $tmp = array();
                $tmp['mac'] = '';
                $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->get('log_access');
                
                foreach($total AS $ell=>$lle)
                {
                    // Đếm tổng
                    $dem = $dem + 1;
                    if($lle['mac'] != $tmp['mac'])
                    {
                        // số người dùng
                        $tmp['mac'] = $lle['mac'];
                        $dem_user = $dem_user + 1;
                    }
                }
                
                $xs[$el]->total_visit = $dem;
                $xs[$el]->user_visit = $dem_user;
                
                $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_facebook');
                $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mail');
                $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->where_gt('time_vs',$time_1)->where_lt('time_vs',$time_2)->count('log_mobile');
                $click = $total_facebook + $total_mail + $total_mobile;
                
                @$outClick = round((($dem - $click) / $dem) * 100);
                @$_click = round(($click / $dem) * 100);
                
                $xs[$el]->personNoClick = $outClick;
                $xs[$el]->personClick = $_click;
                
                /** get tong thơi zan trang dic và so lan hien thi trang dic*/
                $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
                $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
                $xs[$el]->landing_page = $_data_landing_page->landing_page;
                $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
                $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
            }
        }else{
            foreach ( $xs AS $el=>$le ){
                $dem = 0;
                $dem_user = 0;
                $tmp = array();
                $tmp['mac'] = '';
                $total = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => $le->id_shop ))->get('log_access');
                foreach($total AS $ell=>$lle)
                {
                    // Đếm tổng
                    $dem = $dem + 1;
                    if($lle['mac'] != $tmp['mac'])
                    {
                        // số người dùng
                        $tmp['mac'] = $lle['mac'];
                        $dem_user = $dem_user + 1;
                    }
                }
                
                $xs[$el]->total_visit = $dem;
                $xs[$el]->user_visit = $dem_user;
                
                $total_facebook = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_facebook');
                $total_mail = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mail');
                $total_mobile = $this->m_db->where(array('id_campaign' => intval($le->id), 'id_shop' => intval($le->id_shop) ))->count('log_mobile');
                $click = $total_facebook + $total_mail + $total_mobile;
                
                @$outClick = round((($dem - $click) / $dem) * 100);
                @$_click = round(($click / $dem) * 100);
                
                $xs[$el]->personNoClick = $outClick;
                $xs[$el]->personClick = $_click;
                
                /** get tong thơi zan trang dic và so lan hien thi trang dic*/
                $_data_landing_page = $this->db->query('SELECT landing_page, time_landing_page, welcome_page, time_welcome_page FROM campaign WHERE id = '.intval($le->id))->row_object();
                $xs[$el]->time_landing_page = $_data_landing_page->time_landing_page;
                $xs[$el]->landing_page = $_data_landing_page->landing_page;
                $xs[$el]->welcome_page = $_data_landing_page->welcome_page;
                $xs[$el]->time_welcome_page = $_data_landing_page->time_welcome_page;
            }
        }
        
        return array('data' => $xs);
    }
    
    /*public function getListCustomers()
    {
        $data = $this->db->query("SELECT id,username,fullname FROM user WHERE agency = ".$this->session->userdata('id_user'))->result_array();
        return $data;
    }*/
}